﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Edm;
using Obsidian.Data.Sql;
using Oak.Model;

namespace Oak.Core.Data
{
    public class AppKeyDA
    {

        public List<CoActAppKeyInfo> SelectList()
        {
            CoActAppKeyInfo k = new CoActAppKeyInfo();
            IDbHandler dbh = k.CreateHandler();
            dbh.SetFields(k.Fields);
            DataReader dr = dbh.Select();
            List<CoActAppKeyInfo> list = new List<CoActAppKeyInfo>();
            while (dr.Read())
            {
                CoActAppKeyInfo appKey = new CoActAppKeyInfo();
                dr.ReadTo(appKey);
                list.Add(appKey);
            }
            dr.Close();
            return list;
        }


        public List<CoActAppKeyInfo> SelectList(CoActAppKeyQry qry, out ListAttrInfo listAttr)
        {

            CoActAppKeyInfo k = new CoActAppKeyInfo();
            IDbHandler dbh = k.CreateHandler();
            dbh.SetFields(k.Fields);
            DataReader dr = dbh.Select(qry, out listAttr);
            List<CoActAppKeyInfo> list = new List<CoActAppKeyInfo>();
            while (dr.Read())
            {
                CoActAppKeyInfo appKey = new CoActAppKeyInfo();
                dr.ReadTo(appKey);
                list.Add(appKey);
            }
            dr.Close();
            return list;
        }

        public CoActAppKeyInfo SelectAppKey(string key)
        {

            CoActAppKeyInfo k = new CoActAppKeyInfo();
            IDbHandler dbh = k.CreateHandler();
            dbh.SetFields(k.Fields);
            dbh.Where(k.AppKey.Equals(key));
            DataReader dr = dbh.Select();
            if (dr.Read())
            {
                dr.ReadTo(k);
                dr.Close();
                return k;
            }
            else
            {
                dr.Close();
                return null;
            }

        }

    }
}
